﻿Imports System.IO
Imports AjaxControlToolkit

Public Class AddShopOutlets
    Inherits System.Web.UI.Page

    Public dbmanager As DBManager = New DBManager()
    Private Shared businessID As Integer
    Private Shared imageID As Integer
    Private checkOutlets As Boolean
    Private Shared datecreated As DateTime
    'To set masterpage 
    Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        'Check if user is null
        If (Session("user") IsNot Nothing) Then
            'Retrieve userAcc details from session
            Dim userLoggedIn As userAcc = Session("user")
            'Check if user is admin, assign masterpage respectively
            If userLoggedIn.isAdmin Then
                Me.MasterPageFile = "~/MasterPage/Admin.Master"
            Else
                Me.MasterPageFile = "~/MasterPage/User.Master"
            End If
        Else
            'For anonymous users
            Me.MasterPageFile = "~/MasterPage/User.Master"
            'Enable this line to prevent anonymous users from entering this page
            'Response.Redirect("~/Login.aspx")
        End If
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'disable all the textbox
        tbxBusinessName.Enabled = False
        ddlCategory.Enabled = False
        tbxShopURL.Enabled = False
        tbxShopDetails.Enabled = False
        tbxShopOutletName.Enabled = False
        tbxOutletAddress.Enabled = False
        tbxShopOutletTel.Enabled = False
        tbxOpeningHours.Enabled = False
        tbxRemarks.Enabled = False
        fileuploadImage.Enabled = False
        btnUpload.Enabled = False
        AddNewCompanybtn.Enabled = False
        AddNewCompanybtn.ToolTip = "Only after you select the type of business"
        AddAnotherNewCompany.Enabled = False
        AddAnotherNewCompany.ToolTip = "Only after you select the type of business"
        BtnAddMoreOutlets.Enabled = False
        BtnAddMoreOutlets.ToolTip = "Only after you select the type of business"

        If Request.QueryString("bID") IsNot Nothing Then
            Dim businessID = Request.QueryString("bID")
        End If

        'if the user is login , the user is able to see the recommend page 
        'if not the user is not able to see and the label will appear to notify them 
        If (Session("user") IsNot Nothing) Then
            RecommendPanel.Visible = True
            LoginLabel.Visible = False
        Else
            RecommendPanel.Visible = False
            LoginLabel.Visible = True
        End If

    End Sub

    Protected Sub rblBusinessType_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles rblBusinessType.SelectedIndexChanged
        ' deciding whether is it a new or exsisting company 
        If (rblBusinessType.SelectedIndex = 0) Then
            ' if the user selected a new business , the search box will not be visible 
            SearchPanel.Visible = False
            SearchResultPanel.Visible = False
            ' for the user to key in the company details 
            tbxBusinessName.Enabled = True
            ddlCategory.Enabled = True
            tbxShopURL.Enabled = True
            tbxShopDetails.Enabled = True
            ' only after getting the input from the user for the company details then the user is able to key in the branches details.
            tbxShopOutletName.Enabled = True
            tbxOutletAddress.Enabled = True
            tbxShopOutletTel.Enabled = True
            tbxOpeningHours.Enabled = True
            tbxRemarks.Enabled = True
            AddNewCompanybtn.Enabled = True
            AddAnotherNewCompany.Enabled = False
            AddAnotherNewCompany.ToolTip = "Only after you add a company."
            BtnAddMoreOutlets.Enabled = False
            BtnAddMoreOutlets.ToolTip = "Only after you add an branch."
            ' enable the file upload 
            fileuploadImage.Enabled = False
            'hide the label 
            lbDefault.Visible = False

        Else
            ' the search box will be visible and the user are able to search for exsisting company
            SearchPanel.Visible = True
            SearchResultPanel.Visible = True
            ' only after selecting one of the exsisting company the user is able to key in the company details
            tbxBusinessName.Enabled = False
            ddlCategory.Enabled = False
            tbxShopURL.Enabled = False
            tbxShopDetails.Enabled = False
            tbxShopOutletName.Enabled = False
            tbxOutletAddress.Enabled = False
            tbxShopOutletTel.Enabled = False
            tbxOpeningHours.Enabled = False
            tbxRemarks.Enabled = False
            AddAnotherNewCompany.Enabled = False
            AddAnotherNewCompany.ToolTip = "Only after you add a company."
            BtnAddMoreOutlets.Enabled = True
            BtnAddMoreOutlets.ToolTip = "Only after you add a company."
            AddNewCompanybtn.Enabled = False
            AddNewCompanybtn.ToolTip = "Only for a new company."
            'disable the file upload 
            fileuploadImage.Enabled = True
            'hide the label 
            lbDefault.Visible = False

        End If

        'making sure that the business and outlet details is clear
        tbxBusinessName.Text = ""
        ddlCategory.SelectedValue = "Any Categories"
        tbxShopURL.Text = ""
        tbxShopDetails.Text = ""

        tbxShopOutletName.Text = ""
        tbxOutletAddress.Text = ""
        tbxShopOutletTel.Text = ""
        tbxOpeningHours.Text = ""
        tbxRemarks.Text = ""

        hfBusinessID.Value = 0
        hfImageID.Value = 0
        'refresh the gridview 
        GridView1.DataBind()
    End Sub

    Protected Sub SearchResult_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewItemEventArgs) Handles SearchResult.SelectedIndexChanged
        'setting the search result
        ' the name as a linkbtn for user to click so that the exsisting business details will appear 
        Dim lbname As LinkButton = e.Item.FindControl("lbname")
        ' setting it as a hyperlink so that the user can be redirect to the business website 
        Dim hlWebsite As HyperLink = e.Item.FindControl("hlWebsite")
        Dim BusinessDetails As Label = e.Item.FindControl("BusinessDetails")

    End Sub

    Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSearch.Click

    End Sub

    Protected Sub SearchResult_ItemCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewCommandEventArgs) Handles SearchResult.ItemCommand
        'when the company name is selected in the search result 
        If (e.CommandName = "selectBusiness") Then
            ' getting the company details from the database
            Dim businessData = dbmanager.getBusinessDetailsBID(Convert.ToInt32(e.CommandArgument))
            ' input the company details taken from the database
            tbxBusinessName.Text = businessData(0)
            ddlCategory.SelectedItem.Text = businessData(1)
            tbxShopURL.Text = businessData(2)
            tbxShopDetails.Text = businessData(3)
            businessID = e.CommandArgument
            'getting the branches details from the database
            Dim outletData = dbmanager.getBusinessDetailsBID(Convert.ToInt32(e.CommandArgument))
            hfBusinessID.Value = e.CommandArgument
            'get the imageID 
            imageID = dbmanager.getBusinessImage(businessID)
            If Not imageID = 0 Then
                If Not businessID = 0 Then
                    ImgCompany.ImageUrl = ("IHttpHandler.ashx?id=" + imageID.ToString)
                    lbDefault.Visible = False
                End If
            Else
                'Set default user image 
                If imageID = 0 Then
                    imageID = 3
                    ImgCompany.ImageUrl = "IHttpHandler.ashx?id=" + imageID.ToString
                    lbDefault.Visible = True
                End If
            End If
            'refresh the gridview 
            GridView1.DataBind()
            ' enable the textbox so that the user can input the branches details
            tbxShopOutletName.Enabled = True
            tbxOutletAddress.Enabled = True
            tbxShopOutletTel.Enabled = True
            tbxOpeningHours.Enabled = True
            tbxRemarks.Enabled = True

        End If
    End Sub
    'uploading image
    Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnUpload.Click
        hfCheckUpload.Value = "Click"
        Dim userLoggedIn As userAcc = Session("user")
        If (userLoggedIn.verified) Then


                'set the label as false 
                lblBusinessNameValidator.Visible = False
                lblCategoryValidator.Visible = False
                lblValidatorON.Visible = False
                lblValidatorOA.Visible = False
                lblValidatorOP.Visible = False


            'getting the image details
                Dim imgLength As Integer = fileuploadImage.PostedFile.ContentLength
                Dim img As HttpPostedFile = fileuploadImage.PostedFile
            Dim imgName As String = Path.GetFileName(fileuploadImage.PostedFile.FileName)

            'checking if the imgname has the image extention 
            If imgName.Contains(".jpg") Or imgName.Contains(".gif") Or imgName.Contains(".png") Or imgName.Contains(".jpeg") Or imgName.Contains(".bmp") Then
                hfImageID.Value = dbmanager.addImage(imgName, img, imgLength).ToString()
            Else
                MsgBox("Please enter your image in the format of jpg, gif, png, jpeg or bmp")
            End If

            'adding the image to the database 
                hfImageID.Value = dbmanager.addImage(imgName, img, imgLength).ToString()
                MsgBox("Your image have been upload successfully ", MsgBoxStyle.OkOnly, "Confirmation")
                'check the field
                If (tbxBusinessName.Text = "" Or ddlCategory.SelectedItem.Text = "Any Categories" Or tbxShopOutletName.Text = "" Or tbxOutletAddress.Text = "" Or tbxOpeningHours.Text = "") Then
                    If (tbxBusinessName.Text = "") Then
                        lblBusinessNameValidator.Visible = True
                    End If
                    If (ddlCategory.SelectedItem.Text = "Any Categories") Then
                        lblCategoryValidator.Visible = True
                    End If
                    If (tbxShopOutletName.Text = "") Then
                        lblValidatorON.Visible = True
                    End If
                    If (tbxOutletAddress.Text = "") Then
                        lblValidatorOA.Visible = True
                    End If
                    If (tbxOpeningHours.Text = "") Then
                        lblValidatorOP.Visible = True
                    End If

                    'the textbox should remain as the enable for the user to key in all the following necessary details
                    tbxBusinessName.Enabled = True
                    ddlCategory.Enabled = True
                    tbxShopURL.Enabled = True
                    tbxShopDetails.Enabled = True
                    tbxShopOutletName.Enabled = True
                    tbxOutletAddress.Enabled = True
                    tbxShopOutletTel.Enabled = True
                    tbxOpeningHours.Enabled = True
                    tbxRemarks.Enabled = True
                Else
                    'the textbox should remain as the enable for the user to key in all the following necessary details
                    tbxBusinessName.Enabled = True
                    ddlCategory.Enabled = True
                    tbxShopURL.Enabled = True
                    tbxShopDetails.Enabled = True
                    tbxShopOutletName.Enabled = True
                    tbxOutletAddress.Enabled = True
                    tbxShopOutletTel.Enabled = True
                    tbxOpeningHours.Enabled = True
                    tbxRemarks.Enabled = True
                End If
            Else
                MsgBox("Please kindly select a file before pressing the upload button. A default image will be set if there is no image. ", MsgBoxStyle.OkOnly, "Confirmation")
            'check the format
            If (tbxBusinessName.Text = "" Or ddlCategory.SelectedItem.Text = "Any Categories" Or tbxShopOutletName.Text = "" Or tbxOutletAddress.Text = "" Or tbxOpeningHours.Text = "") Then
                If (tbxBusinessName.Text = "") Then
                    lblBusinessNameValidator.Visible = True
                End If
                If (ddlCategory.SelectedItem.Text = "Any Categories") Then
                    lblCategoryValidator.Visible = True
                End If
                If (tbxShopOutletName.Text = "") Then
                    lblValidatorON.Visible = True
                End If
                If (tbxOutletAddress.Text = "") Then
                    lblValidatorOA.Visible = True
                End If
                If (tbxOpeningHours.Text = "") Then
                    lblValidatorOP.Visible = True
                End If

                'the textbox should remain as the enable for the user to key in all the following necessary details
                tbxBusinessName.Enabled = True
                ddlCategory.Enabled = True
                tbxShopURL.Enabled = True
                tbxShopDetails.Enabled = True
                tbxShopOutletName.Enabled = True
                tbxOutletAddress.Enabled = True
                tbxShopOutletTel.Enabled = True
                tbxOpeningHours.Enabled = True
                tbxRemarks.Enabled = True
            End If

                imageID = hfImageID.Value

                If Not imageID = 0 Then
                    'If Not businessID = 0 Then
                    ImgCompany.ImageUrl = "IHttpHandler.ashx?id=" + imageID.ToString
                    lbDefault.Visible = False

                End If

                'Set default user image 
                If imageID = 0 Then
                    imageID = 3
                    ImgCompany.ImageUrl = "IHttpHandler.ashx?id=" + imageID.ToString
                    lbDefault.Visible = True
                End If
            End If
    End Sub

    Protected Sub BtnAddMoreOutlets_Click(ByVal sender As Object, ByVal e As EventArgs) Handles BtnAddMoreOutlets.Click
        Dim userLoggedIn As userAcc = Session("user")
        If (userLoggedIn.verified) Then
            'get the image 
            imageID = dbmanager.getBusinessImageID(hfImageID.Value)
            If Not imageID = 0 Then
                If Not businessID = 0 Then
                    ImgCompany.ImageUrl = "IHttpHandler.ashx?id=" + imageID.ToString
                    lbDefault.Visible = False
                End If
            Else

                'Set default user image 
                If imageID = 0 Then
                    imageID = 3
                    ImgCompany.ImageUrl = "IHttpHandler.ashx?id=" + imageID.ToString
                    lbDefault.Visible = True
                End If
            End If
        Else
            MsgBox("Please verify your account before you are able to add new review. Please check your email for the verification code")
        End If

        fileuploadImage.Enabled = False
        ' enable the textbox so that the user can input the branches details
        tbxShopOutletName.Enabled = True
        tbxOutletAddress.Enabled = True
        tbxShopOutletTel.Enabled = True
        tbxOpeningHours.Enabled = True
        tbxRemarks.Enabled = True
        ' declare the date as today's date 
        Dim datecreated As Date
        datecreated = Today.Date
        lblValidatorON.Visible = False
        lblValidatorOA.Visible = False
        lblValidatorOP.Visible = False
        'if the tbxShopOutletName or tbxOutletAddress or  tbxOpeningHours is empty 
        If (tbxShopOutletName.Text = "" Or tbxOutletAddress.Text = "" Or tbxOpeningHours.Text = "") Then
            If (tbxShopOutletName.Text = "") Then
                lblValidatorON.Visible = True
            End If
            'if the
            If (tbxOutletAddress.Text = "") Then
                lblValidatorOA.Visible = True
            End If


            If (tbxOpeningHours.Text = "") Then
                lblValidatorOP.Visible = True
            End If

            tbxShopOutletName.Enabled = True
            tbxOutletAddress.Enabled = True
            tbxShopOutletTel.Enabled = True
            tbxOpeningHours.Enabled = True
            tbxRemarks.Enabled = True
        Else
            If (dbmanager.addOutletDetails(businessID, tbxShopOutletName.Text, tbxOutletAddress.Text, tbxOpeningHours.Text, tbxRemarks.Text, 0, tbxShopOutletTel.Text, datecreated)
                ) Then
                ' clear all the textboxes
                tbxShopOutletName.Text = ""
                tbxOutletAddress.Text = ""
                tbxShopOutletTel.Text = ""
                tbxOpeningHours.Text = ""
                tbxRemarks.Text = ""
                ' disable the validator
                lblValidatorON.Visible = False
                lblValidatorOA.Visible = False
                lblValidatorOP.Visible = False
                'enable the add another company
                AddNewCompanybtn.Enabled = False
                AddAnotherNewCompany.Enabled = True
                MsgBox("You have successfully added a new branch to the company. Do you still want to add a new branch?", MsgBoxStyle.YesNo, "Successfully added a new outlet")
                'if the user wants a new branch 
                If (MsgBoxResult.Yes) Then
                    'refresh of gridview
                    GridView1.DataBind()
                    ' enable the textbox for the user to key in the branch details
                    tbxShopOutletName.Enabled = True
                    tbxOutletAddress.Enabled = True
                    tbxShopOutletTel.Enabled = True
                    tbxOpeningHours.Enabled = True
                    tbxRemarks.Enabled = True
                    'if the user do not wants a new branch 
                Else
                    'refresh of gridview
                    GridView1.DataBind()
                    tbxShopOutletName.Enabled = False
                    tbxOutletAddress.Enabled = False
                    tbxShopOutletTel.Enabled = False
                    tbxOpeningHours.Enabled = False
                    tbxRemarks.Enabled = False
                End If
            End If
        End If
    End Sub

    Protected Sub AddNewCompanybtn_Click(ByVal sender As Object, ByVal e As EventArgs) Handles AddNewCompanybtn.Click
        lblBusinessNameValidator.Visible = False
        lblCategoryValidator.Visible = False
        lblValidatorON.Visible = False
        lblValidatorOA.Visible = False
        lblValidatorOP.Visible = False

        Dim userLoggedIn As userAcc = Session("user")
        If (userLoggedIn.verified) Then
            'if there us a file upload 
            If (fileuploadImage.HasFile) Then
                MsgBox("Are you sure you want to upload this image ?", MsgBoxStyle.YesNo, "Confirmation")
                If (MsgBoxResult.Yes) Then
                    'check the fields 
                    If (tbxBusinessName.Text = "" Or ddlCategory.SelectedItem.Text = "Any Categories" Or tbxShopOutletName.Text = "" Or tbxOutletAddress.Text = "" Or tbxOpeningHours.Text = "") Then
                        If (tbxBusinessName.Text = "") Then
                            lblBusinessNameValidator.Visible = True
                        End If
                        If (ddlCategory.SelectedItem.Text = "Any Categories") Then
                            lblCategoryValidator.Visible = True
                        End If
                        If (tbxShopOutletName.Text = "") Then
                            lblValidatorON.Visible = True
                        End If
                        If (tbxOutletAddress.Text = "") Then
                            lblValidatorOA.Visible = True
                        End If
                        If (tbxOpeningHours.Text = "") Then
                            lblValidatorOP.Visible = True
                        End If

                        tbxBusinessName.Enabled = True
                        ddlCategory.Enabled = True
                        tbxShopURL.Enabled = True
                        tbxShopDetails.Enabled = True
                        tbxShopOutletName.Enabled = True
                        tbxOutletAddress.Enabled = True
                        tbxShopOutletTel.Enabled = True
                        tbxOpeningHours.Enabled = True
                        tbxRemarks.Enabled = True
                        'if it is all fill in 
                    Else
                        MsgBox("Are you sure you completed adding the new company? ", MsgBoxStyle.YesNo, "Confirmation of adding new company")
                        If (MsgBoxResult.Yes) Then
                            'upload the image 
                            Dim imgLength As Integer = fileuploadImage.PostedFile.ContentLength
                            Dim img As HttpPostedFile = fileuploadImage.PostedFile
                            Dim imgName As String = Path.GetFileName(fileuploadImage.PostedFile.FileName)
                            hfImageID.Value = dbmanager.addImage(imgName, img, imgLength).ToString()
                            MsgBox("Your image have been upload successfully ", MsgBoxStyle.OkOnly, "Confirmation")

                            'Adding the user input in the database for business
                            Dim datecreated As DateTime
                            datecreated = DateTime.Now
                            Dim visits As Integer
                            visits = 0
                            ' adding business details
                            businessID = dbmanager.addBusinessDetails(tbxBusinessName.Text, ddlCategory.SelectedItem.Text, tbxShopURL.Text, tbxShopDetails.Text, datecreated, visits)
                            imageID = dbmanager.getBusinessImageID(hfImageID.Value)
                            ' assigning the image to the company 
                            hfBusinessID.Value = businessID
                            imageID = Convert.ToInt32(hfImageID.Value)
                            dbmanager.assignImageBusiness(imageID, businessID)

                            If Not imageID = 0 Then
                                If Not businessID = 0 Then
                                    ImgCompany.ImageUrl = ("IHttpHandler.ashx?id=" + imageID.ToString)
                                    lbDefault.Visible = False
                                End If
                            Else
                                'Set default user image 
                                If imageID = 0 Then
                                    imageID = 3
                                    ImgCompany.ImageUrl = "IHttpHandler.ashx?id=" + imageID.ToString
                                    lbDefault.Visible = True
                                End If
                            End If

                            If (dbmanager.addOutletDetails(businessID, tbxShopOutletName.Text, tbxOutletAddress.Text, tbxOpeningHours.Text, tbxRemarks.Text, 0, tbxShopOutletTel.Text, datecreated)
                                   ) Then
                                MsgBox("Successfully added branches", MsgBoxStyle.OkOnly, "Success")

                                'refresh of gridview
                                GridView1.DataBind()
                                'get image 
                                If Not imageID = 0 Then
                                    If Not businessID = 0 Then
                                        ImgCompany.ImageUrl = "IHttpHandler.ashx?id=" + imageID.ToString
                                        lbDefault.Visible = False

                                    End If

                                    'Set default user image 
                                    If imageID = 0 Then
                                        imageID = 3
                                        ImgCompany.ImageUrl = "IHttpHandler.ashx?id=" + imageID.ToString
                                        lbDefault.Visible = True
                                    End If
                                    'clear the textboxes
                                    tbxShopOutletName.Text = ""
                                    tbxOutletAddress.Text = ""
                                    tbxShopOutletTel.Text = ""
                                    tbxOpeningHours.Text = ""
                                    tbxRemarks.Text = ""
                                    'enable the textbox 
                                    tbxShopOutletName.Enabled = True
                                    tbxOutletAddress.Enabled = True
                                    tbxShopOutletTel.Enabled = True
                                    tbxOpeningHours.Enabled = True
                                    tbxRemarks.Enabled = True
                                    BtnAddMoreOutlets.Enabled = True
                                    fileuploadImage.Enabled = False
                                    AddNewCompanybtn.Enabled = False
                                    AddAnotherNewCompany.Enabled = True
                                    ' hide the validator
                                    lblBusinessNameValidator.Visible = False
                                    lblCategoryValidator.Visible = False
                                    lblValidatorON.Visible = False
                                    lblValidatorOA.Visible = False
                                    lblValidatorOP.Visible = False

                                End If
                            End If
                        End If
                    End If
                End If
            Else
                ' if click on upload button before 
                If (hfCheckUpload.Value = "Click") Then
                    MsgBox("The above image is correct", MsgBoxStyle.YesNo, "Confirmation")
                    If (MsgBoxResult.Yes) Then
                        'check the fields 
                        If (tbxBusinessName.Text = "" Or ddlCategory.SelectedItem.Text = "Any Categories" Or tbxShopOutletName.Text = "" Or tbxOutletAddress.Text = "" Or tbxOpeningHours.Text = "") Then
                            If (tbxBusinessName.Text = "") Then
                                lblBusinessNameValidator.Visible = True
                            End If
                            If (ddlCategory.SelectedItem.Text = "Any Categories") Then
                                lblCategoryValidator.Visible = True
                            End If
                            If (tbxShopOutletName.Text = "") Then
                                lblValidatorON.Visible = True
                            End If
                            If (tbxOutletAddress.Text = "") Then
                                lblValidatorOA.Visible = True
                            End If
                            If (tbxOpeningHours.Text = "") Then
                                lblValidatorOP.Visible = True
                            End If

                            tbxBusinessName.Enabled = True
                            ddlCategory.Enabled = True
                            tbxShopURL.Enabled = True
                            tbxShopDetails.Enabled = True
                            tbxShopOutletName.Enabled = True
                            tbxOutletAddress.Enabled = True
                            tbxShopOutletTel.Enabled = True
                            tbxOpeningHours.Enabled = True
                            tbxRemarks.Enabled = True
                            'if it is all fill in 
                        Else
                            MsgBox("Are you sure you completed adding the new company? ", MsgBoxStyle.YesNo, "Confirmation of adding new company")
                            If (MsgBoxResult.Yes) Then
                                'Adding the user input in the database for business
                                Dim datecreated As DateTime
                                datecreated = DateTime.Now
                                Dim visits As Integer
                                visits = 0
                                ' adding business details
                                businessID = dbmanager.addBusinessDetails(tbxBusinessName.Text, ddlCategory.SelectedItem.Text, tbxShopURL.Text, tbxShopDetails.Text, datecreated, visits)
                                imageID = dbmanager.getBusinessImageID(hfImageID.Value)
                                ' assigning the image to the company 
                                hfBusinessID.Value = businessID
                                imageID = Convert.ToInt32(hfImageID.Value)
                                dbmanager.assignImageBusiness(imageID, businessID)

                                If Not imageID = 0 Then
                                    If Not businessID = 0 Then
                                        ImgCompany.ImageUrl = ("IHttpHandler.ashx?id=" + imageID.ToString)
                                        lbDefault.Visible = False

                                    End If
                                Else
                                    'Set default user image 
                                    If imageID = 0 Then
                                        imageID = 3
                                        ImgCompany.ImageUrl = "IHttpHandler.ashx?id=" + imageID.ToString
                                        lbDefault.Visible = True
                                    End If
                                End If

                                If (dbmanager.addOutletDetails(businessID, tbxShopOutletName.Text, tbxOutletAddress.Text, tbxOpeningHours.Text, tbxRemarks.Text, 0, tbxShopOutletTel.Text, datecreated)
                                       ) Then
                                    MsgBox("Successfully added branches", MsgBoxStyle.OkOnly, "Success")

                                    'refresh of gridview
                                    GridView1.DataBind()
                                    'clear the textboxes
                                    tbxShopOutletName.Text = ""
                                    tbxOutletAddress.Text = ""
                                    tbxShopOutletTel.Text = ""
                                    tbxOpeningHours.Text = ""
                                    tbxRemarks.Text = ""
                                    'enable the textbox 
                                    tbxShopOutletName.Enabled = True
                                    tbxOutletAddress.Enabled = True
                                    tbxShopOutletTel.Enabled = True
                                    tbxOpeningHours.Enabled = True
                                    tbxRemarks.Enabled = True
                                    BtnAddMoreOutlets.Enabled = True
                                    fileuploadImage.Enabled = False
                                    btnUpload.Enabled = False
                                    AddNewCompanybtn.Enabled = False
                                    AddAnotherNewCompany.Enabled = True

                                    ' hide the validator
                                    lblBusinessNameValidator.Visible = False
                                    lblCategoryValidator.Visible = False
                                    lblValidatorON.Visible = False
                                    lblValidatorOA.Visible = False
                                    lblValidatorOP.Visible = False


                                End If
                            End If
                        End If
                    End If
                Else
                    MsgBox("No file is uploaded", MsgBoxStyle.OkOnly, "Confirmation")
                    If (tbxBusinessName.Text = "" Or ddlCategory.SelectedItem.Text = "Any Categories" Or tbxShopOutletName.Text = "" Or tbxOutletAddress.Text = "" Or tbxOpeningHours.Text = "") Then
                        If (tbxBusinessName.Text = "") Then
                            lblBusinessNameValidator.Visible = True
                        End If
                        If (ddlCategory.SelectedItem.Text = "Any Categories") Then
                            lblCategoryValidator.Visible = True
                        End If
                        If (tbxShopOutletName.Text = "") Then
                            lblValidatorON.Visible = True
                        End If
                        If (tbxOutletAddress.Text = "") Then
                            lblValidatorOA.Visible = True
                        End If
                        If (tbxOpeningHours.Text = "") Then
                            lblValidatorOP.Visible = True
                        End If

                        tbxBusinessName.Enabled = True
                        ddlCategory.Enabled = True
                        tbxShopURL.Enabled = True
                        tbxShopDetails.Enabled = True
                        tbxShopOutletName.Enabled = True
                        tbxOutletAddress.Enabled = True
                        tbxShopOutletTel.Enabled = True
                        tbxOpeningHours.Enabled = True
                        tbxRemarks.Enabled = True
                        'if it is all fill in 
                    Else
                        MsgBox("Are you sure you completed adding the new company? ", MsgBoxStyle.YesNo, "Confirmation of adding new company")
                        If (MsgBoxResult.Yes) Then
                            'Adding the user input in the database for business
                            Dim datecreated As DateTime
                            datecreated = DateTime.Now
                            Dim visits As Integer
                            visits = 0
                            ' adding business details
                            businessID = dbmanager.addBusinessDetails(tbxBusinessName.Text, ddlCategory.SelectedItem.Text, tbxShopURL.Text, tbxShopDetails.Text, datecreated, visits)
                            imageID = dbmanager.getBusinessImageID(hfImageID.Value)
                            ' assigning the image to the company 
                            hfBusinessID.Value = businessID
                            imageID = Convert.ToInt32(hfImageID.Value)
                            dbmanager.assignImageBusiness(imageID, businessID)

                            If Not imageID = 0 Then
                                If Not businessID = 0 Then
                                    ImgCompany.ImageUrl = ("IHttpHandler.ashx?id=" + imageID.ToString)
                                    lbDefault.Visible = False
                                End If
                            Else
                                'Set default user image 
                                If imageID = 0 Then
                                    imageID = 3
                                    ImgCompany.ImageUrl = "IHttpHandler.ashx?id=" + imageID.ToString
                                    lbDefault.Visible = True

                                End If
                            End If

                            If (dbmanager.addOutletDetails(businessID, tbxShopOutletName.Text, tbxOutletAddress.Text, tbxOpeningHours.Text, tbxRemarks.Text, 0, tbxShopOutletTel.Text, datecreated)
                                   ) Then
                                MsgBox("Successfully added branches", MsgBoxStyle.OkOnly, "Success")



                                'refresh of gridview
                                GridView1.DataBind()
                                'clear the textboxes
                                tbxShopOutletName.Text = ""
                                tbxOutletAddress.Text = ""
                                tbxShopOutletTel.Text = ""
                                tbxOpeningHours.Text = ""
                                tbxRemarks.Text = ""
                                'enable the textbox 
                                tbxShopOutletName.Enabled = True
                                tbxOutletAddress.Enabled = True
                                tbxShopOutletTel.Enabled = True
                                tbxOpeningHours.Enabled = True
                                tbxRemarks.Enabled = True
                                BtnAddMoreOutlets.Enabled = True
                                fileuploadImage.Enabled = False
                                AddNewCompanybtn.Enabled = False
                                AddAnotherNewCompany.Enabled = True
                                ' hide the validator
                                lblBusinessNameValidator.Visible = False
                                lblCategoryValidator.Visible = False
                                lblValidatorON.Visible = False
                                lblValidatorOA.Visible = False
                                lblValidatorOP.Visible = False


                            End If
                        End If
                    End If
                End If
            End If
        Else
            MsgBox("Please verify your account before you are able to add new review. Please check your email for the verification code")
        End If

    End Sub

    Protected Sub AddAnotherNewCompany_Click(ByVal sender As Object, ByVal e As EventArgs) Handles AddAnotherNewCompany.Click
        fileuploadImage.Enabled = True
        btnUpload.Enabled = True
        ' if the user selected a new business , the search box will not be visible 
        SearchPanel.Visible = False
        SearchResultPanel.Visible = False
        ' for the user to key in the company details 
        tbxBusinessName.Enabled = True
        ddlCategory.Enabled = True
        tbxShopURL.Enabled = True
        tbxShopDetails.Enabled = True
        tbxBusinessName.Text = ""
        ddlCategory.SelectedValue = "Any Categories"

        tbxShopURL.Text = ""
        tbxShopDetails.Text = ""


        ' only after getting the input from the user for the company details then the user is able to key in the branches details.
        tbxShopOutletName.Enabled = True
        tbxOutletAddress.Enabled = True
        tbxShopOutletTel.Enabled = True
        tbxOpeningHours.Enabled = True
        tbxRemarks.Enabled = True
        tbxShopOutletName.Text = ""
        tbxOutletAddress.Text = ""
        tbxShopOutletTel.Text = ""
        tbxOpeningHours.Text = ""
        tbxRemarks.Text = ""

        Dim userLoggedIn As userAcc = Session("user")
        If (userLoggedIn.verified) Then
            If (rblBusinessType.SelectedIndex = 0) Then
                ImgCompany.ImageUrl = ""
                lbDefault.Visible = False
            Else
                If Not imageID = 0 Then
                    If Not businessID = 0 Then
                        ImgCompany.ImageUrl = ("IHttpHandler.ashx?id=" + imageID.ToString)
                        lbDefault.Visible = False
                    End If
                Else
                    'Set default user image 
                    If imageID = 0 Then
                        imageID = 3
                        ImgCompany.ImageUrl = "IHttpHandler.ashx?id=" + imageID.ToString
                        lbDefault.Visible = True

                    End If
                End If
            End If
        Else
            MsgBox("Please verify your account before you are able to add new review. Please check your email for the verification code")
        End If
        hfBusinessID.Value = 0
        hfImageID.Value = 0
        'refresh the gridview 
        GridView1.DataBind()

        AddNewCompanybtn.Enabled = True
        BtnAddMoreOutlets.Enabled = False
        BtnAddMoreOutlets.ToolTip = "Only after you add an outlet."
        AddAnotherNewCompany.Enabled = False
        AddAnotherNewCompany.ToolTip = "Only after adding the company"


    End Sub




End Class